package demo.thread;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class Demo1 {
    public static void main1(String[] args) {
        Thread t1 = new Thread(){
            @Override
            public void run() {
                System.out.println("通过匿名内部类创建线程");;
            }
        };
        t1.start();
        Thread t2 = new Thread(() -> System.out.println("通过lambda表达式创建线程"));
        t2.start();
    }
    public static void main2(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("通过匿名内部类创建线程");
            }
        });
        Thread t2 = new Thread(() -> System.out.println("通过 lambda 表达式创建线程"));
    }

    public static void main3(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable = new Callable<Integer>(){
            @Override
            public Integer call() throws Exception {
                int n = 10;
                while (n-- > 0){
                    Thread.sleep(1000);
                }
                return 1;
            }
        };
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        Thread t = new Thread(futureTask);
        t.start();
        int result = futureTask.get();
        System.out.println(result);
    }

    public static void main4(String[] args) {
        ThreadFactory factory = new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r){
                    @Override
                    public void run() {
                        System.out.println("创建线程");
                    }
                };
                return t;
            }
        };
    }

    public static void main5(String[] args) throws ExecutionException, InterruptedException {
        AtomicInteger count = new AtomicInteger(0);
        ExecutorService service = Executors.newFixedThreadPool(3);
        service.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " 执行第 " + count.incrementAndGet() + " 个任务");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Future<?> submit = service.submit(new Callable<AtomicInteger>() {
            @Override
            public AtomicInteger call() throws Exception {
                System.out.println(Thread.currentThread().getName() + " 执行第 " + count.incrementAndGet() + " 个任务");
                int i = 10;
                while(i-- > 0) {
                    count.incrementAndGet();
                }
                return count;
            }
        });
        System.out.println(submit.get());
    }

    public static void main6(String[] args) {
        AtomicInteger count = new AtomicInteger(0);
        ExecutorService service = Executors.newFixedThreadPool(2);
        service.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " 执行第 " + count.incrementAndGet() + " 个任务");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " 执行第 " + count.incrementAndGet() + " 个任务");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " 执行第 " + count.incrementAndGet() + " 个任务");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }
    public static void main7(String[] args) {
        AtomicInteger count = new AtomicInteger(0);
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " 执行第 " + count.incrementAndGet() + " 个任务");
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " 执行第 " + count.incrementAndGet() + " 个任务");
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " 执行第 " + count.incrementAndGet() + " 个任务");
            }
        });service.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " 执行第 " + count.incrementAndGet() + " 个任务");
            }
        });

    }

    public static void main8(String[] args) {
        AtomicInteger count = new AtomicInteger(0);
        ExecutorService service = Executors.newSingleThreadExecutor();
        service.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " 执行第 " + count.incrementAndGet() + " 个任务");
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " 执行第 " + count.incrementAndGet() + " 个任务");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });service.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " 执行第 " + count.incrementAndGet() + " 个任务");
            }
        });
    }

    public static void main(String[] args) {
        AtomicInteger count = new AtomicInteger(0);
        ExecutorService service = Executors.newScheduledThreadPool(100);
    }
}
